//
//  YbrPlayHistoryDao.m
//  wifibox
//
//  Created by xinle on 15/9/1.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "YbrPlayHistoryDao.h"
@implementation YbrPlayHistoryDao

+(BOOL)insert:(YbrMusicItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        
        //NSString *sql = [NSString stringWithFormat:@""];
        NSString *sqlFormat = @"insert into playHistory (musicid ,musicName, singername,albumId,albumname ,downloadurl,file,img ,duration,type ,timerstr,timeLength,lastPlayTime) VALUES (%d,%@,%@,%d,%@,%@,%@,%@,%d,%d,%@,%@,%@)";
        rs = [db executeQueryWithFormat:sqlFormat, item.musicId,item.musicName,item.singername,item.albumId,item.albumname,item.downloadurl,item.file,item.img,item.duration,item.type,item.timeStr,item.timeLength,item.lastPlayTime];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
}


+(NSArray *)query
{
    __block NSArray *arr;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from playHistory order by _id desc"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrMusicItem *musicItem     = [[YbrMusicItem alloc] init];
            musicItem._id               = [rs intForColumn:@"_id"];
            musicItem.musicId           = [rs intForColumn:@"musicid"];
            musicItem.musicName         = [rs stringForColumn:@"musicName"];
            musicItem.singername        = [rs stringForColumn:@"singername"];
            musicItem.albumId           = [rs intForColumn:@"albumId"];
            musicItem.albumname         = [rs stringForColumn:@"albumname"];
            musicItem.downloadurl       = [rs stringForColumn:@"downloadurl"];
            musicItem.file              = [rs stringForColumn:@"file"];
            musicItem.img               = [rs stringForColumn:@"img"];
            musicItem.duration          = [rs intForColumn:@"duration"];
            musicItem.type              = [rs intForColumn:@"type"];
            musicItem.timeStr           = [rs stringForColumn:@"timerstr"];
            musicItem.timeLength        = [rs stringForColumn:@"timeLength"];
            musicItem.lastPlayTime      = [rs stringForColumn:@"lastPlayTime"];
            [tempArr addObject:musicItem];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
}

+(BOOL)deletesWithId:(YbrMusicItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from playHistory where _id = %i",item._id];
    }];
    return isOk;
}

+(BOOL)deletesWithUrl:(YbrMusicItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from playHistory where file = %@",item.file];
    }];
    return isOk;
}

+(BOOL)deletesWithDownUrl:(YbrMusicItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from playHistory where downloadurl = %@",item.downloadurl];
    }];
    return isOk;
}

+(BOOL)deletAll
{
    __block BOOL isOk;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"delete from playHistory"];
    }];
    return isOk;
}

@end
